/**
 * 
 */
package com.ms.player;

import java.math.BigDecimal;
import java.util.HashMap;

import com.ms.game.common.container.LevelInfoBinUtil;
import com.ms.service.module.zone.command.BasicDebugCommand;
import com.ms.service.module.zone.command.CommandHelper;
import com.ms.service.module.zone.module.BasicZoneModule;
import com.xpec.c4.db.dao.PlayerLevelCalculator;
import com.xpec.c4.game.common.Helper;
import com.xpec.c4.game.datamanager.BaseDataInfo;

/**
 * 設定角色等級的指令
 * 
 * @author TW15010
 */
public class SetLevelCommand extends BasicDebugCommand {
	private CommandHelper helper = new CommandHelper(getName());

	public SetLevelCommand() {
	}

	@Override
	public String execute() {
		if (!(commandValue.length == 2 || commandValue.length == 3)) {
			return getDescription();
		}

		int level = getParamToInt(1);
		if (level < 0) {
			return getDescription();
		}

		long expSub = 0L;
		if(getParamToInt(2) == 1){
			level++;
			expSub = 1;
		}
		
		BigDecimal exp = Helper.sub(LevelInfoBinUtil.getExpByLevel(0, level), new BigDecimal(expSub));
		HashMap<String, Long> info = LevelInfoBinUtil.getExpInfos(exp);
		
		player = getPlayer();
		player.setLevel(info.get(PlayerLevelCalculator.LEVEL).intValue());
		player.setExp(exp);
		
		UpdateAttributeList updateList = new UpdateAttributeList();
		updateList.addUpdateValue(PlayerAttributeType.LEVEL, player.getLevel());
		updateList.addUpdateValue(PlayerAttributeType.EXP, player.getExp());

		BasicZoneModule module = getModule();
		if (module != null) {
			module.getPlayerDataAgent().updatePlayerDataToDC2(player, updateList, this,
					"updatePlayerDataToDCCallback",
					player);
		}

		return "proc ....";
	}
	
	public void updatePlayerDataToDCCallback(BaseDataInfo obj, Object... userObject) {
		Player player = (Player) userObject[0];
		player.sendUpdatedAttrToClient();
		BasicDebugCommand.sendDebugCommandResponse(player.getSessionID(), "ok");
	}

	@Override
	public String getName() {
		return "setlv";
	}

	@Override
	public String getDescription() {
		return getName() + " [level]";
	}

	@Override
	public String getShortDescription() {
		return "set player level.";
	}
}
